package neusoft.yuejian_11.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import neusoft.yuejian_11.base.BaseFragment;
import neusoft.yuejian_11.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class MainFragment extends BaseFragment {
    private RadioGroup radioGroup;

    private ClubListFragment clubListFragment;
    private TrainListFragment trainListFragment;
    private HomeFragment homeFragment;
    private PostListFragment postListFragment;
    private MyFragment myFragment;

    private FragmentManager manager;
    private FragmentTransaction transaction;


    public MainFragment() {
        // Required empty public constructor
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        radioGroup = (RadioGroup) view.findViewById(R.id.radioGroup);

        manager = getChildFragmentManager();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.radio_club_list:
                        clubListFragment = new ClubListFragment();
                        replace(clubListFragment);
                        break;
                    case R.id.radio_train_list:
                        trainListFragment = new TrainListFragment();
                        replace(trainListFragment);
                        break;
                    case  R.id.radio_home:
                        homeFragment = new HomeFragment();
                        replace(homeFragment);
                        break;
                    case  R.id.radio_post_list:
                        postListFragment = new PostListFragment();
                        replace(postListFragment);
                        break;
                    case  R.id.radio_my:
                        myFragment = new MyFragment();
                        replace(myFragment);
                        break;
                }
            }
        });
        radioGroup. check(R.id.radio_club_list);

    }

    private void replace(Fragment fragment){
        transaction = manager.beginTransaction();
        transaction.replace(R.id.frameLayout,fragment);
        transaction.commit();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

}
